{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using dataclasses instead of functions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dataclasses\n",
    "import typing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<itertools.islice at 0x10b899908>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "computation = itertools.islice(itertools.accumulate(itertools.count(10)), 10)\n",
    "computation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mType:\u001b[0m        islice\n",
       "\u001b[0;31mString form:\u001b[0m <itertools.islice object at 0x10b899908>\n",
       "\u001b[0;31mDocstring:\u001b[0m  \n",
       "islice(iterable, stop) --> islice object\n",
       "islice(iterable, start, stop[, step]) --> islice object\n",
       "\n",
       "Return an iterator whose next() method returns selected values from an\n",
       "iterable.  If start is specified, will skip all preceding elements;\n",
       "otherwise, start defaults to zero.  Step defaults to one.  If\n",
       "specified as another value, step determines how many values are \n",
       "skipped between successive calls.  Works like a slice() on a list\n",
       "but returns an iterator.\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "computation?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have no idea what this function will do till we execute it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 21, 33, 46, 60, 75, 91, 108, 126, 145]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(computation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclasses.dataclass\n",
    "class Count:\n",
    "    start: int\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return self.gen()\n",
    "    \n",
    "    def gen(self):\n",
    "        i = self.start\n",
    "        while True:\n",
    "            yield i\n",
    "            i += 1\n",
    "\n",
    "\n",
    "@dataclasses.dataclass\n",
    "class Accumulate:\n",
    "    iterable: typing.Iterable\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return self.gen()\n",
    "    \n",
    "    def gen(self):\n",
    "        s = 0\n",
    "        for x in self.iterable:\n",
    "            s += x\n",
    "            yield s\n",
    "\n",
    "@dataclasses.dataclass\n",
    "class ISlice:\n",
    "    iterable: typing.Iterable\n",
    "    stop: int\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return self.gen()\n",
    "    \n",
    "    def gen(self):\n",
    "        n = 0\n",
    "        for x in self.iterable:\n",
    "            yield x\n",
    "            n += 1\n",
    "            if n == self.stop:\n",
    "                return\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_computation = ISlice(Accumulate(Count(10)), 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 21, 33, 46, 60, 75, 91, 108, 126, 145]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(new_computation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ISlice(iterable=Accumulate(iterable=Count(start=10)), stop=10)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_computation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Can also do this for functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclasses.dataclass\n",
    "class Map:\n",
    "    fn: typing.Callable\n",
    "    \n",
    "    def __call__(self, xs):\n",
    "        return [self.fn(x) for x in xs]\n",
    "\n",
    "@dataclasses.dataclass\n",
    "class Filter:\n",
    "    fn: typing.Callable\n",
    "    \n",
    "    def __call__(self, xs):\n",
    "        return [x for x in xs if self.fn(x)]\n",
    "\n",
    "@dataclasses.dataclass\n",
    "class Compose:\n",
    "    a: typing.Callable\n",
    "    b: typing.Callable\n",
    "    \n",
    "    def __call__(self, xs):\n",
    "        return self.a(self.b(xs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Compose(a=Filter(fn=<function is_even at 0x10bbf2f28>), b=Map(fn=<function add_two at 0x10b84b158>))"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_even(i):\n",
    "    return i % 2 == 0\n",
    "\n",
    "def add_two(i):\n",
    "    return i + 2\n",
    "\n",
    "f = Compose(Filter(is_even), Map(add_two))\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f([1, 2, 3, 4, 5, 6, ])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can \"see inside\" this computation. Why is this useful?\n",
    "\n",
    "We could later optimize this series of operations, by traversing the datastructrues.\n",
    "\n",
    "Conclusion:\n",
    "* Python can do FP fine.\n",
    "* If you treat function composition as data, then your computation becomes less opaque, but it can be just the same for the user.\n",
    "* Python gives you some tools to hide what your object does from what it is. Make use of these to give your users more bang for their buck.\n",
    "* Much better for debugging."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
